package com.cjj;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.render.Path;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.hiviewdfx.HiLog;

import java.math.BigDecimal;

public class MaterialWaveView extends Component implements MaterialHeadListener, Component.DrawTask {
    private int DefaulWaveHeight = Contants.getInstance().getDefaulWaveHeight();
    private int DefaulHeadHeight = Contants.getInstance().getDefaulHeadHeight();
    private int waveHeight;
    private int headHeight;
    private Path path;
    private Paint paint;
    private int color;

    public MaterialWaveView(Context context) {
        this(context, null);
    }

    public MaterialWaveView(Context context, AttrSet attrs) {
        this(context, attrs, null);
    }

    public MaterialWaveView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
//        setWillNotDraw(false);
        path = new Path();
        paint = new Paint();
        paint.setAntiAlias(true);
        addDrawTask(this::onDraw);
    }

    public int getColor() {
        return color;
    }

    public void setColor(int color) {
        this.color = color;
        invalidate();
    }

    public int getHeadHeight() {
        return headHeight;
    }

    public void setHeadHeight(int headHeight) {
        this.headHeight = headHeight;
    }

    public int getWaveHeight() {
        return waveHeight;
    }

    public void setWaveHeight(int waveHeight) {
        this.waveHeight = waveHeight;
    }

    public int getDefaulWaveHeight() {
        return DefaulWaveHeight;
    }

    public void setDefaulWaveHeight(int defaulWaveHeight) {
        DefaulWaveHeight = defaulWaveHeight;
    }

    public int getDefaulHeadHeight() {
        return DefaulHeadHeight;
    }

    public void setDefaulHeadHeight(int defaulHeadHeight) {
        DefaulHeadHeight = defaulHeadHeight;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        path.reset();
        paint.setColor(new Color(color));
        path.lineTo(0, headHeight);
        path.quadTo(getEstimatedWidth() / (float) 2, headHeight + waveHeight, getEstimatedWidth(), headHeight);
        path.lineTo(getEstimatedWidth(), 0);
        canvas.drawPath(path, paint);
    }

    @Override
    public void onComlete(MaterialRefreshLayout br) {
        waveHeight = 0;
        AnimatorValue animator = new AnimatorValue();
        animator.setDuration(200);
//        animator.setInterpolator(new DecelerateInterpolator());
        animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
//                int value = (int) animatorValue.getAnimatedValue();
//                headHeight = (int) ((1-value)*headHeight);
                headHeight = getIntValue((1 - (double) value) * headHeight);
                invalidate();
            }
        });
        animator.start();
    }

    @Override
    public void onBegin(MaterialRefreshLayout br) {

    }

    @Override
    public void onPull(MaterialRefreshLayout br, float fraction) {
        setHeadHeight((int) (Util.dip2px(getContext(), DefaulHeadHeight) * Util.limitValue(1, fraction)));
        setWaveHeight((int) (Util.dip2px(getContext(), DefaulWaveHeight) * Math.max(0, new BigDecimal(fraction).subtract(new BigDecimal(1)).floatValue())));
        HiLog.info(Contants.LABEL, "DefaulHeadHeight==" + DefaulHeadHeight + "==DefaulWaveHeight=" + DefaulWaveHeight);
        invalidate();
    }

    @Override
    public void onRelease(MaterialRefreshLayout br, float fraction) {

    }

    @Override
    public void onRefreshing(MaterialRefreshLayout br) {
        setHeadHeight((int) (Util.dip2px(getContext(), DefaulHeadHeight)));
        int waveHeight = getWaveHeight();
        AnimatorValue animator = new AnimatorValue();
        animator.setValueUpdateListener(new AnimatorValue.ValueUpdateListener() {
            @Override
            public void onUpdate(AnimatorValue animatorValue, float value) {
                System.out.println(value + "------onRefreshing");
                setWaveHeight(getIntValue((1 - (double) value) * waveHeight));
                invalidate();
            }
        });
//        animator.setInterpolator(new BounceInterpolator());
        animator.setCurveType(Animator.CurveType.BOUNCE);
        animator.setDuration(200);
        animator.start();
    }

    private int getIntValue(double value) {
        BigDecimal bigDecimal = new BigDecimal(value).setScale(2, BigDecimal.ROUND_HALF_DOWN);
        return bigDecimal.intValue();
    }

}
